#include <iostream>
#include <stdio.h>
#include "vft.hpp"

using namespace std;

class Base1 {
public:
	virtual void f() { cout << "Base1::f" << endl; }
	virtual void g() { cout << "Base1::g" << endl; }
	virtual void h() { cout << "Base1::h" << endl; }
};

class Base2 {
public:
	virtual void f() { cout << "Base2::f" << endl; }
	virtual void g() { cout << "Base2::g" << endl; }
	virtual void h() { cout << "Base2::h" << endl; }
};

class Derive1 : public Base1, public Base2 {
	virtual void g1() { cout << "Derive1::g1" << endl; }
	virtual void h1() { cout << "Derive1::h1" << endl; }
};

class Derive2 : public Base1, public Base2 {
	virtual void f() { cout << "Derive2::f" << endl; }
	virtual void g1() { cout << "Derive2::g1" << endl; }
};

int
main() {
	// 继承而不覆盖父类中的虚函数
	Derive1 *d1 = new Derive1();
	AccessVFT(d1, 0); // 子类的虚函数存放在第一个虚函数表中
	printf("*****\n");
	AccessVFT(d1, 1); // 访问第二个虚函数表
	delete d1;

	// 继承并且覆盖父类中的虚函数
	// 此时会调用 Derive2::f
	cout << endl << endl;
	Derive2 *d2 = new Derive2();
	AccessVFT(d2, 0); // Base1::f 被子类覆盖，总共有 4 个虚函数
	printf("*****\n");
	AccessVFT(d2, 1); // Base2::f 被子类覆盖
	delete d2;
	return 0;
}
